package cn.ibdsr.human.controller;

import cn.ibdsr.human.constant.BizConstants;
import cn.ibdsr.human.constant.Const;
import cn.ibdsr.human.constant.Result;
import cn.ibdsr.human.db.model.Company;
import cn.ibdsr.human.db.model.User;
import cn.ibdsr.human.db.model.UserDetail;
import cn.ibdsr.human.exception.BizExceptionEnum;
import cn.ibdsr.human.exception.BusinessException;
import cn.ibdsr.human.manager.LogFormatManager;
import cn.ibdsr.human.service.CompanyService;
import cn.ibdsr.human.service.JobService;
import cn.ibdsr.human.service.UserDetailService;
import cn.ibdsr.human.service.UserService;
import cn.ibdsr.human.transfer.PasswordDTO;
import cn.ibdsr.human.transfer.UserDetailDTO;
import cn.ibdsr.human.transfer.UserDetailSaveDTO;
import cn.ibdsr.human.transfer.company.*;
import cn.ibdsr.human.transfer.job.*;
import cn.ibdsr.human.transfer.platform.*;
import cn.ibdsr.human.util.RegUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @description:
 * @author: cdl
 * @email: iamcimon@qq.com
 * @date: 2021/8/4 16:26
 */
@Api(tags = "平台控制器")
@RestController
@RequestMapping(value = "/platforms")
@Slf4j
@Validated
public class PlatformController {
    @Resource
    private UserService userService;
    @Resource
    private JobService jobService;
    @Resource
    private UserDetailService userDetailService;
    @Resource
    private CompanyService companyService;

    @Resource
    private LogFormatManager logFormatManager;
    /**
     * cdl-ok
     * 获取个人信息
     */
    @ApiOperation("获取平台个人信息")
    @GetMapping("/platforms")
    @ResponseBody//
    public Result<PlatformUserDetialDTO> getSelf(@RequestAttribute(name = Const.USER_ID) Long userId) {
        logFormatManager.formatKeyPointLog("获取平台个人信息","userId="+userId, BizConstants.MODULE_PLATFORM);
        // Long userId=1L;
        PlatformUserDetialDTO userDetailDTO = userService.getPlatformUser(userId);
        return new Result<>(userDetailDTO);
    }

    /**
     * cdl-ok
     * 获取个人信息
     */
    @ApiOperation("修改平台用户名称")
    @PutMapping("/platforms/names")
    @ResponseBody//
    public Result<Long> updateUserName(@RequestAttribute(name = Const.USER_ID) Long userId, @RequestParam("name") String name) {
        // Long userId=1L;
        logFormatManager.formatKeyPointLog("修改平台用户名称","userId="+userId+": name="+name, BizConstants.MODULE_PLATFORM);
        Long flag = userService.updateUserName(userId, name);
        return new Result<>(flag);
    }









    ////////////////////////////针对岗位列表相关操作//////////////////////////////////////////////

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("平台分页获取岗位信息")
    @GetMapping("/jobs")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<JobUserResponseDTO> getJobList(@Valid JobQueryDTO jobQueryDTO) {
        logFormatManager.formatKeyPointLog("平台分页获取岗位信息",  JSONObject.toJSONString(jobQueryDTO), BizConstants.MODULE_PLATFORM);
        JobUserResponseDTO flag = jobService.getPlatformJobList(jobQueryDTO);
        return new Result<>(flag);
    }
    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("发布岗位/取消发布")
    @PutMapping("/jobs/publish-statuses/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> publishJobOrNot(@RequestAttribute(name = Const.USER_ID  ) Long userId, @PathVariable("id") Long id, @RequestBody JobOperatorDTO jobSaveDTO) {
        jobSaveDTO.setId(id);
        logFormatManager.formatKeyPointLog("发布岗位/取消发布","userId="+userId+":"+JSONObject.toJSONString(jobSaveDTO), BizConstants.MODULE_PLATFORM);
        Long flag = jobService.publishJobOrNot(userId, jobSaveDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("删除岗位")
    @DeleteMapping("/jobs/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> delete(@RequestAttribute(name = Const.USER_ID  ) Long userId, @PathVariable("id") Long id) {
        logFormatManager.formatKeyPointLog("删除岗位","userId="+userId+": id="+id, BizConstants.MODULE_PLATFORM);
        Long flag = jobService.delete(userId, id);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("编辑岗位")
    @PutMapping("/jobs/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> editJob(@RequestAttribute(name = Const.USER_ID  ) Long userId, @PathVariable("id") Long id, @RequestBody @Valid JobSaveDTO jobSaveDTO) {
        jobSaveDTO.setId(id);
        logFormatManager.formatKeyPointLog("编辑岗位","userId="+userId+": "+JSONObject.toJSONString(jobSaveDTO), BizConstants.MODULE_PLATFORM);
        Long flag = jobService.editJob(userId, jobSaveDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("获取岗位详情")
    @GetMapping("/jobs/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<JobResponseDTO> getJobDetail(@PathVariable("id") Long id) {
        logFormatManager.formatKeyPointLog("获取岗位详情","id="+id, BizConstants.MODULE_PLATFORM);
        JobResponseDTO flag = jobService.getJobDetail(id);
        return new Result<>(flag);
    }
    /**
     * cdl-ok
     * 修改密码
     *
     * @param resetPasswordDTO
     */
    @ApiOperation("平台重置（用户|企业）密码")
    @PutMapping("/users/{uid}/passwords")
    @ResponseBody
    public Result<Long> resetPwd(@RequestAttribute(name = Const.USER_ID) Long userId,@PathVariable("uid") Long uid,
                                 @Valid ResetPasswordDTO resetPasswordDTO) {
        resetPasswordDTO.setId(uid);
        logFormatManager.formatKeyPointLog("平台重置（用户|企业）密码","userId="+userId+": "+JSONObject.toJSONString(resetPasswordDTO), BizConstants.MODULE_PLATFORM);
        Long flag = userService.resetPwd(resetPasswordDTO, userId);
        return new Result<>(flag);
    }
    ////////////////////////针对求职者的接口////////////////////

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("平台分页获取用户信息列表")
    @GetMapping("/users")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<UserListResponseDTO> getUserList(@RequestAttribute(name = Const.USER_ID  ) Long userId, @Valid UserQueryDTO userQueryDTO) {
        logFormatManager.formatKeyPointLog("平台分页获取用户信息列表","userId="+userId+": "+JSONObject.toJSONString(userQueryDTO), BizConstants.MODULE_PLATFORM);
        UserListResponseDTO flag = userDetailService.getUserList(userQueryDTO);
        return new Result<>(flag);
    }
    /**
     * cdl-ok
     * 获取个人信息
     */
    @ApiOperation("获取个人信息")
    @GetMapping("/users/{uid}")
    @ResponseBody//
    public Result<UserDetailDTO> getUserDetail(@PathVariable("uid") Long uid) {
        logFormatManager.formatKeyPointLog("获取个人信息","uid="+uid, BizConstants.MODULE_PLATFORM);
        UserDetailDTO userDetailDTO = userDetailService.getUserDetail(uid);
        return new Result<>(userDetailDTO);
    }
//
    /**
     * cdl-ok
     * 保存个人信息
     */
    @ApiOperation("保存个人信息")
    @PutMapping("/users/{uid}")
    @ResponseBody
    public Result<Long> saveUserInformation(@RequestAttribute(name = Const.USER_ID) Long userId, @PathVariable("uid") Long uid,@RequestBody @Valid UserDetailSaveDTO userDetailSaveDTO) {
        userDetailSaveDTO.setId(uid);
        logFormatManager.formatKeyPointLog("保存个人信息","userId="+userId+": "+JSONObject.toJSONString(userDetailSaveDTO), BizConstants.MODULE_PLATFORM);
        if (!userDetailSaveDTO.getIdCardNo().matches(RegUtils.ID_CARD_NO)) {
            throw new BusinessException(BizExceptionEnum.TOURIST_ID_CARD_NO_IS_ERROR);
        }
        Long flag = userDetailService.saveByPlatform(userId, userDetailSaveDTO);
        return new Result<>(flag);
    }

    ////////////////////////针对企业的相关接口////////////////////
    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("平台分页获取企业列表")
    @GetMapping("/companies")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<CompanyListDTO> getCompanies(@Valid CompanyQueryDTO companyQueryDTO) {
        logFormatManager.formatKeyPointLog("平台分页获取企业列表", JSONObject.toJSONString(companyQueryDTO), BizConstants.MODULE_PLATFORM);
        CompanyListDTO flag = companyService.getCompanies(companyQueryDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 获取个人信息
     */
    @ApiOperation("获取企业信息")
    @GetMapping("/companies/{id}")
    @ResponseBody//
    public Result<CompanyResponseDTO> getCompanyInformation(@PathVariable("id") Long id) {
        // Long userId=1L;
        logFormatManager.formatKeyPointLog("获取企业信息", "id="+id, BizConstants.MODULE_PLATFORM);
        CompanyResponseDTO companyResponseDTO=companyService.platFormGetCompanyInformation(id);
        return new Result<>(companyResponseDTO);
    }

    /**
     * cdl-ok
     * 保存个人信息
     */
    @ApiOperation("认证企业")
    @PutMapping("/companies/{id}")
    @ResponseBody
    public Result<Long> identifyCompany(@RequestAttribute(name = Const.USER_ID  ) Long userId, @PathVariable("id") Long id,
                                    @RequestBody @Valid PlatfromIdentifyCompanyDTO companySaveDTO) {
        companySaveDTO.setId(id);
        logFormatManager.formatKeyPointLog("认证企业", "userI="+userId+": "+JSONObject.toJSONString(companySaveDTO), BizConstants.MODULE_PLATFORM);
        Long flag=companyService.identifyCompany(userId,companySaveDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 保存个人信息
     */
    @ApiOperation("编辑企业信息")
    @PutMapping("/companies/edit/{id}")
    @ResponseBody
    public Result<Long> platformSaveCompany(@RequestAttribute(name = Const.USER_ID  ) Long userId,
                             @PathVariable("id") Long id,
                             @RequestBody @Valid PlatfromIdentifyCompanyDTO companySaveDTO) {

        companySaveDTO.setId(id);
        logFormatManager.formatKeyPointLog("编辑企业信息", "userI="+userId+": "+JSONObject.toJSONString(companySaveDTO), BizConstants.MODULE_PLATFORM);
        Long flag=companyService.platformSaveCompany(userId,companySaveDTO);
        return new Result<>(flag);
    }
}
